
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.Task;
import com.crv.ci.service.ITaskService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class TaskAction extends AbstractAdminController<ITaskService>{

    @Autowired
    private ITaskService taskService;
    
    @RequestMapping(value="/task/list")
    public String list(Task task,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(task); 	
    	BizData4Page<Task> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("task", task);
    	return "/module/task/list";
    }    
    
    @RequestMapping(value="/task/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("task", new Task());
        return "module/task/edit";
    }    
    
    @RequestMapping(value="/task/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	Task task = taskService.fetch(id);
    	model.addAttribute("task", task);
        return "module/task/edit";
    }    
    
    @RequestMapping(value="/task/save")
    @ResponseBody
    public String save(HttpServletRequest request,Task task){
		boolean isAddNew = StringUtils.isBlank(task.getId())?true:false;
		if(isAddNew)
		{
			task.setId(IdGenerator.createNewId());
			taskService.add(task);
		}
		else
		{
			taskService.update(task);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/task/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	taskService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(Task task)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("jobId", task.getJobId());		
		conditions.put("agentId", task.getAgentId());		
		conditions.put("clientIp", task.getClientIp());		
		conditions.put("status", task.getStatus());		
		conditions.put("note", task.getNote());		
		conditions.put("returnMsg", task.getReturnMsg());		
		conditions.put("startDate", task.getStartDate());		
		conditions.put("endDate", task.getEndDate());		
		conditions.put("createrId", task.getCreaterId());		
		conditions.put("createrName", task.getCreaterName());		
		conditions.put("createDate", task.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ITaskService getMainService() {
        return taskService;
    }

    @Override
    protected String getMainObjName() {
        return "task";
    }

    @Override
    protected String getViewTitle() {
        return "task";
    }
}
